package me.linczh.bank.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.linczh.bank.bean.AccountBean;
import me.linczh.bank.bean.EmployeeBean;
import me.linczh.bank.bean.LogBean;
import me.linczh.bank.bean.OperatorBean;
import me.linczh.bank.bean.enums.Department;
import me.linczh.bank.bean.enums.Operation;
import me.linczh.bank.dao.DaoFactory;
import me.linczh.bank.dao.LogDao;
import me.linczh.framework.query.Query;

public class LogService {
	private LogDao logDao = DaoFactory.getLogDao();
	
	public void log(Operation operation, AccountBean account, OperatorBean operator, double change, EmployeeBean employee) {
		LogBean bean = new LogBean();
		bean.setOperation(operation);
		bean.setAccount(account.getAccount());
		bean.setEmployee(employee.getEmployee());
		bean.setEmployeename(employee.getUsername());
		bean.setDepartment(employee.getDepartment());
		bean.setChange(change);
		bean.setBalance(account.getBalance());
		if (operator != null) {
			bean.setIdentity(operator.getIdentity());
			bean.setUsername(operator.getUsername());
		} else {
			bean.setIdentity(account.getIdentity());
			bean.setUsername(account.getUsername());
		}
		bean = logDao.create(bean);
	}
	
	public List<LogBean> showlog() {
		return logDao.query(new Query<LogBean>() {
			@Override
			public boolean matched(LogBean item) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from log_tbl";
			}			
		});
	}
	
	public List<LogBean> accountLog(AccountBean account, Date start, Date end) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("account", account.getAccount());
		params.put("start", start);
		Calendar c = Calendar.getInstance();
		c.setTime(end);
		c.add(Calendar.DATE, 1);
		params.put("end", c.getTime());
		return logDao.query(new Query<LogBean>() {
			Map<String, Object> map = params;
			@Override
			public boolean matched(LogBean item) {
				// TODO Auto-generated method stub
				if (map.get("account").equals(item.getAccount())) {
					if (item.getTime().after((Date) map.get("start")) && item.getTime().before((Date) map.get("end"))) return true;
				}
				return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from log_tbl";
			}
		});
	}
	
	public List<LogBean> logReport(int datetype, String dateStr, String monthStr, int quarter,
			String yearStr, int querytype, String employee, String department) {
		String format;
		String queryStr;
		final Map<String, String> params = new HashMap<String, String>();
		Query<LogBean> query = null;
		switch (datetype) {
		case 0:	// date
			format = "yyyy-MM-dd";
			queryStr = dateStr;
			break;
		case 1: // month
			format = "yyyy-MM";
			queryStr = monthStr;
			break;
		case 2: // quarter
			format = "yyyy-MM";
			queryStr = "";
			for (int i = 1; i <= 3; i++) queryStr += String.format(yearStr + "-%2d", quarter * 3 + i) + " || ";
			break;
		default:
			format = "yyyy";
			queryStr = yearStr;
		}
		System.out.println("format: " + format + " & string: " + queryStr);
		params.put("format", format);
		params.put("query", queryStr);
		switch (querytype) {
		case 0:	// employee
			query = new Query<LogBean>() {
				Map<String, String> map = params;
				@Override
				public boolean matched(LogBean item) {
					// TODO Auto-generated method stub
					SimpleDateFormat fmt = new SimpleDateFormat(map.get("format"));
					if (map.get("employee").equals(item.getEmployee()))
						if (map.get("query").contains(fmt.format(item.getTime()))) return true;
					return false;
				}
				@Override
				public String statement() {
					// TODO Auto-generated method stub
					return "select * from log_tbl";
				}
			};
			params.put("employee", employee);
			break;
		case 1:
			query = new Query<LogBean>() {
				Map<String, String> map = params;
				@Override
				public boolean matched(LogBean item) {
					// TODO Auto-generated method stub
					SimpleDateFormat fmt = new SimpleDateFormat(map.get("format"));
					if (map.get("department").equals(item.getDepartment().name()))
						if (map.get("query").contains(fmt.format(item.getTime()))) return true;
					return false;
				}
				@Override
				public String statement() {
					// TODO Auto-generated method stub
					return "select * from log_tbl";
				}
			};
			params.put("department", department);
			break;
		default:
			query = new Query<LogBean>() {
				Map<String, String> map = params;
				@Override
				public boolean matched(LogBean item) {
					// TODO Auto-generated method stub
					SimpleDateFormat fmt = new SimpleDateFormat(map.get("format"));
					if (map.get("query").contains(fmt.format(item.getTime()))) return true;
					return false;
				}
				@Override
				public String statement() {
					// TODO Auto-generated method stub
					return "select * from log_tbl";
				}
			};
		}
		return logDao.query(query);
	}
	
	public List<LogBean> employeeLog(EmployeeBean employee, String dateFormat, String date) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("employee", employee.getEmployee());
		params.put("format", new SimpleDateFormat(dateFormat));
		params.put("date", date);
		return logDao.query(new Query<LogBean>() {
			@Override
			public boolean matched(LogBean item) {
				Map<String, Object> map = params;
				// TODO Auto-generated method stub
				if (map.get("employee").equals(item.getEmployee())) {
					SimpleDateFormat fmt = (SimpleDateFormat)map.get("format");
					String dateString = (String)map.get("date");
					if (fmt.format(item.getTime()).equals(dateString)) return true;
				}
				return false;
			}

			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from log_tbl";
			}
		});
	}
	
	public List<LogBean> departmentLog(Department department, Date date, int type) {
		return null;
	}
	
	public List<LogBean> bankLog(Date date, int type) {
		return null;
	}
}
